package com.zzedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzThesaurusWords;
import com.zzedu.dto.ZzThesaurusWordsDTO;
import com.zzedu.params.ZzThesaurusWordsAllParams;
import com.zzedu.service.IZzThesaurusWordsService;
import com.zzedu.vo.ZzThesaurusWordsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 单词管理Controller
 *
 * @author Mr.Wang
 * @date 2025-01-29
 */
@RestController
@RequestMapping("/thesaurus/words")
public class ZzThesaurusWordsController extends BaseController {
    @Autowired
    private IZzThesaurusWordsService zzThesaurusWordsService;

    /**
     * 查询单词管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzThesaurusWordsDTO zzThesaurusWords) {
        IPage<ZzThesaurusWords> page = zzThesaurusWordsService.page(zzThesaurusWords.getPageDTO(), zzThesaurusWordsService.buildQuery(zzThesaurusWords));
        return getDataTable(page, zzThesaurusWordsService.convertVO(page.getRecords()));
    }

    /**
     * 导出单词管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:export')")
    @Log(title = "单词管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzThesaurusWordsDTO zzThesaurusWords) {
        List<ZzThesaurusWords> list = zzThesaurusWordsService.list(zzThesaurusWordsService.buildQuery(zzThesaurusWords));
        ExcelUtil<ZzThesaurusWordsVO> util = new ExcelUtil<ZzThesaurusWordsVO>(ZzThesaurusWordsVO.class);
        util.exportExcel(ServletUtils.getResponse(), zzThesaurusWordsService.convertVO(list), "单词管理数据");
    }


    /**
     * 获取单词管理详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzThesaurusWordsService.getFormById(id));
    }


    /**
     * 删除单词管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:remove')")
    @Log(title = "单词管理", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzThesaurusWordsService.removeBatchByIds(ids));
    }

    /**
     * 单词上线
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:offline,thesaurus:words:online')")
    @Log(title = "单词管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/online/{ids}")
    public AjaxResult online(@PathVariable List<Long> ids) {
        return toAjax(zzThesaurusWordsService.update(Wrappers.lambdaUpdate(ZzThesaurusWords.class)
                .set(ZzThesaurusWords::getOnLineFlag, CommonEnums.YES.getCode())
                .in(ZzThesaurusWords::getId, ids)));
    }

    /**
     * 单词下线
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:edit')")
    @Log(title = "单词管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/offline/{ids}")
    public AjaxResult offline(@PathVariable List<Long> ids) {
        return toAjax(zzThesaurusWordsService.update(Wrappers.lambdaUpdate(ZzThesaurusWords.class)
                .set(ZzThesaurusWords::getOnLineFlag, CommonEnums.NO.getCode())
                .in(ZzThesaurusWords::getId, ids)));
    }

    /**
     * 删除属性
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:query')")
    @RequestMapping(value = "/deleteAttrData/{id}")
    @Log(title = "单词管理", businessType = BusinessType.UPDATE)
    public AjaxResult deleteAttrData(@PathVariable("id") Long id) {
        return success(zzThesaurusWordsService.deleteAttrData(id));
    }


    /**
     * 修改保存单词管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('thesaurus:words:edit')")
    @Log(title = "单词管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/allSaveWords")
    public AjaxResult allSaveWords(@Validated ZzThesaurusWordsAllParams zzThesaurusWordsParams)  throws Exception{
        return AjaxResult.success(zzThesaurusWordsService.allSaveWords(zzThesaurusWordsParams));
    }

}
